package com.struts2.example.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.struts2.example.model.Answer;
import com.struts2.example.model.Connect;

public class SupplyAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//private String TAG="SupplyAction :";
	private int questionId;
	private Answer answerSupply;
	private List<Answer> listAnswerSupply;
	public String execute()
	{	
		listAnswerSupply=new ArrayList<Answer>();
		String ret = ERROR;
		Connect conn = new Connect();
		Connection connected = conn.GetConnect();
		
		try {
			String sql = "SELECT * FROM testonline_bang_dai.answer WHERE question_id = ?";
			PreparedStatement ps = connected.prepareStatement(sql);
			ps.setInt(1,getQuestionId());
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				answerSupply=new Answer();
				answerSupply.setAnswerId(rs.getInt(1));
				answerSupply.setAnswerText(rs.getString(3));
				answerSupply.setTypeOfAnswer(rs.getInt(4));
				listAnswerSupply.add(answerSupply);
				ret = SUCCESS;
			}
		} catch (Exception e) {
			ret = ERROR;
		} finally {
			if (connected != null) {
				try {
					connected.close();
				} catch (Exception e) {
				}
			}
		}
		return ret;
	}
	public int getQuestionId()
	{
		return this.questionId;
	}
	
	public void setQuestionId(int questionId)
	{
		this.questionId=questionId;
	}
	public List<Answer> getListAnswerSupply() {
		return listAnswerSupply;
	}
	public void setListAnswerSupply(List<Answer> listAnswerSupply) {
		this.listAnswerSupply = listAnswerSupply;
	}
	public Answer getAnswerSupply() {
		return answerSupply;
	}
	public void setAnswerSupply(Answer answerSupply) {
		this.answerSupply = answerSupply;
	}
}
